//
//  CommoditiesController.m
//  MBObjectServer
//
//  Created by Juvid on 14-4-2.
//  Copyright (c) 2014年 MBpayer. All rights reserved.
//

#import "CommoditiesController.h"
#import "EPOSDetailEntity.h"
@implementation CommoditiesController
-(id)init{
    self=[super init];
    if (!self) {
        return nil;
    }
     baseserver = [[MBBaseServer alloc]init];
    return self;
}
-(void)CancleRequest{
    baseserver.baseclient=nil;
}
#pragma mark SKU扫描查询
-(void)ScanBarCodeIntoBillEntity:(NSString *)BarCode
                             Qty:(NSString *)Qty
                     ContainerID:(NSString *)ContainerID
                    IsJudgePrice:(NSString *)IsJudgePrice
                        StartNum:(int)startNum
                          EndNum:(int)endNum
                         Success:(ReceiveSuccess)processSuccess
                         Failure:(ReceiveFailure)processFailure{
    __block CommoditiesController *selfCtr=self;
    baseserver.isStartLoad=YES;
    NSMutableDictionary* postDic=[[NSMutableDictionary alloc]init];
    [postDic setObject:[DicRequest GetOrgCode] forKey:@"OrgCode"];
    [postDic setObject:BarCode forKey:@"BarCode"];
    [postDic setValue:Qty forKey:@"Qty"];
    [postDic setObject:[DicRequest GetUserID] forKey:@"SellerUserID"];
    [postDic setObject:ContainerID forKey:@"ContainerID"];
    [postDic setObject:IsJudgePrice forKey:@"IsJudgePrice"];
    [postDic setValue:[NSNumber numberWithInt:startNum] forKey:@"StartNum"];
    [postDic setObject:[NSNumber numberWithInt:endNum] forKey:@"EndNum"];
    [baseserver setRequestStyle:nil andSetPathString:@"billsale_handler.ashx" andSetHeadString:nil andActionName:@"ScanBarCodeIntoBillEntity" andSetMainDictionary:postDic block:^(NSDictionary *responseData) {
        [selfCtr ScanBarCodeResult:responseData Success:processSuccess];
    }failblock:^(NSError *error) {
        processFailure(error);
    }];
    
}
-(void)ScanBarCodeResult:(NSDictionary *)responseData Success:(ReceiveSuccess)processSuccess{
    BOOL  Result = [[responseData objectForKey:@"Result"] boolValue];
    NSMutableDictionary *mlDicReturn;
    
    if (!Result) {
        [DicRequest ShowAlert:@"错误" Message:[responseData objectForKey:@"ErrMessage"]];
    }
    else{
        mlDicReturn=[[NSMutableDictionary alloc]init];
        NSArray *arrSale=[responseData objectForKey:@"DetailEntitys"];
        NSMutableArray * promotionResArr = [[NSMutableArray alloc]init ] ;
        for (int i  = 0 ; i <arrSale.count; i ++ )
        {
            NSDictionary * dic = [arrSale objectAtIndex:i];
            EPOSDetailEntity * pModel =  [EPOSDetailEntity getEPOSDetailEntityFromDictionary:dic];
            [promotionResArr addObject:pModel];
        }
        [mlDicReturn setValue:promotionResArr forKey:@"Message"];
        processSuccess(mlDicReturn);
        arrSale=nil;
        promotionResArr=nil;
    }
}
#pragma mark 库存查询
-(void)GetStockJson:(NSString *)ProdID
            Success:(ReceiveSuccess)processSuccess
            Failure:(ReceiveFailure)processFailure{
    __block CommoditiesController *selfCtr=self;

    baseserver.isStartLoad=YES;
    NSMutableDictionary* postDic=[[NSMutableDictionary alloc]init];
    [postDic setObject:[DicRequest GetOrgCode] forKey:@"OrgCode"];
    [postDic setObject:ProdID forKey:@"ProdID"];
    [baseserver setRequestStyle:nil andSetPathString:@"stock_handler.ashx" andSetHeadString:nil andActionName:@"GetStockJson" andSetMainDictionary:postDic block:^(NSDictionary *responseData) {
        [selfCtr GetStockJsonResult:responseData Success:processSuccess];
       
    } failblock:^(NSError *error) {
        processFailure(error);
    }];
    
}
-(void)GetStockJsonResult:(NSDictionary *)responseData Success:(ReceiveSuccess)processSuccess{
    BOOL  Result = [[responseData objectForKey:@"Result"] boolValue];
    NSMutableDictionary *mlDicReturn;
    if (!Result) {
        [DicRequest ShowAlert:@"错误" Message:[responseData objectForKey:@"ErrMessage"]];
    }
    else{
        mlDicReturn=[[NSMutableDictionary alloc]init];
        NSArray *arr=[responseData objectForKey:@"Stocks"];
        if (arr.count>0) {
            //                [self.mlStortNum addObject:[[arr objectAtIndex:0] objectForKey:@"HandStock"]];
            [mlDicReturn setValue:[[arr objectAtIndex:0] objectForKey:@"HandStock"] forKey:@"HandStock"];
        }
        else{
            //               无库存默认给数组加一
            //                [self.mlStortNum addObject:@""];
            [mlDicReturn setValue:@"" forKey:@"HandStock"];
        }
        //            [mlDicReturn setValue:[responseData objectForKey:@"Stocks"] forKey:@"Stocks"];
        processSuccess(mlDicReturn);
        //            arrSale=nil;
    }

}
#pragma mark 交易查询
-(void)RetrieveBillSaleHeads:(NSString *)saleNo
                    StartNum:(int)startNum
                      EndNum:(int)endNum
                     Success:(ReceiveSuccess)processSuccess
                     Failure:(ReceiveFailure)processFailure{
   __block CommoditiesController *selfCtr=self;
    NSMutableDictionary* postDic=[[NSMutableDictionary alloc]init];
    NSString *orgCode = [DicRequest GetOrgCode];
    if (orgCode ==nil)
    {
        UIAlertView *noOrgCode = [[UIAlertView alloc]initWithTitle:nil message:@"获取不到门店号" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [noOrgCode show];
        return;
    }
    [postDic setObject:orgCode forKey:@"OrgCode"];
    if (![saleNo isEqual:@""]) {
        [postDic setObject:saleNo forKey:@"SaleNo"];
    }
    [postDic setValue:[NSNumber numberWithInt:startNum] forKey:@"StartNum"];
    [postDic setObject:[NSNumber numberWithInt:endNum] forKey:@"EndNum"];
    [baseserver setRequestStyle:nil andSetPathString:@"billsale_handler.ashx" andSetHeadString:nil andActionName:@"RetrieveBillSaleHeads" andSetMainDictionary:postDic block:^(NSDictionary *responseData)
    {
        [selfCtr RetrieveBillResult:responseData Success:processSuccess];
    } failblock:^(NSError *error) {
        processFailure(error);
    }];
}
-(void)RetrieveBillResult:(NSDictionary *)responseData Success:(ReceiveSuccess)processSuccess{
    BOOL  Result = [[responseData objectForKey:@"Result"] boolValue];
    NSMutableDictionary *mlDicReturn;
    
    if (!Result) {
        [DicRequest ShowAlert:@"错误" Message:[responseData objectForKey:@"ErrMessage"]];
    }
    else{
        mlDicReturn=[[NSMutableDictionary alloc]init];
        NSArray *arrSale=[responseData objectForKey:@"SaleHeads"];
        NSMutableArray *saleHeadsArr = [[NSMutableArray alloc]init];
        if (arrSale.count>0) {
            for (int i =0; i <[arrSale count]; i++)
            {
                NSMutableDictionary *modelArr = [arrSale objectAtIndex:i];
                EPOSIndentListModel *iLModel = [EPOSIndentListModel getEPOSIndentListModelFromDictionary:modelArr];
                [saleHeadsArr addObject:iLModel];
                iLModel=nil;
            }
            [mlDicReturn setValue:saleHeadsArr forKey:@"Message"];
            processSuccess(mlDicReturn);
            arrSale=nil;
            saleHeadsArr=nil;
        }
        else{
            
            [DicRequest ShowAlert:nil Message:@"无此交易订单"];
        }
        
    }
    

}
#pragma mark 获取小票详情
-(void)RetrieveBillSale:(NSString *)saleID
                  IsPay:(BOOL)isPay
                Success:(ReceiveSuccess)processSuccess
                Failure:(ReceiveFailure)processFailure{
    __block CommoditiesController *selfCtr=self;
    NSMutableDictionary* postDic=[[NSMutableDictionary alloc]init];
    [postDic setObject:saleID forKey:@"SaleID"];
    NSMutableArray * enumArray = [[NSMutableArray alloc]init] ;
    for (int i = 0 ; i < 4 ; i++) {
        NSNumber * enums = [NSNumber numberWithInt:i];
        [enumArray addObject:enums] ;
    }
    [postDic setObject:enumArray forKey:@"BillSaleWrapEnums"];
    [baseserver setRequestStyle:nil andSetPathString:@"billsale_handler.ashx" andSetHeadString:nil andActionName:@"RetrieveBillSale" andSetMainDictionary:postDic
                          block:^(NSDictionary *responseData) {
                              [selfCtr RetrieveBillSaleResult:responseData IsPay:isPay Success:processSuccess];
                          } failblock:^(NSError *error) {
                            processFailure(error);  
                          }];
    
}
-(void)RetrieveBillSaleResult:(NSDictionary *)responseData IsPay:(BOOL)isPay Success:(ReceiveSuccess)processSuccess{
    BOOL  Result = [[responseData objectForKey:@"Result"] boolValue];
    NSMutableDictionary *mlDicReturn;
    if (Result) {
        mlDicReturn=[[NSMutableDictionary alloc]init];
        [mlDicReturn setValue:responseData forKey:@"AllMessage"];
        //            如果不在支付页面
        if (!isPay) {
            NSMutableArray *mlArrReturn=[[NSMutableArray alloc]init];
            [mlDicReturn setValue:[responseData objectForKey:@"HeadEntity"] forKey:@"HeadEntity"];
            [mlDicReturn setValue:[responseData objectForKey:@"DetailEntitys"] forKey:@"DetailEntitys"];
            NSArray * tempPayEntitys  = [responseData objectForKey:@"PayEntitys"];
            NSArray *   temppromotionEntitysArr = [responseData objectForKey:@"PromotionEntitys"];
            for ( NSDictionary * payDic in tempPayEntitys) {
                NSMutableString * payStr = [[NSMutableString alloc]init] ;
                NSString * str1 =[NSString stringWithFormat:@"支付价格:%.2f ,",[[payDic objectForKey:@"ActPayValue"] floatValue]];
                NSString *str=[payDic objectForKey:@"PayCode"];
                if ([str isEqual:@"Card"]) {
                    
                    str=@"卡(Card)";
                }
                else if ([str isEqual:@"Cash"]) {
                    str=@"现金(Cash)";
                }
                else if ([str isEqual:@"CashVouc"]) {
                    str=@"抵用券(CashVouc)";
                }
                else if ([str isEqual:@"Voucher"]) {
                    str=@"代金券(Voucher)";
                }
                NSString * str2 =[NSString stringWithFormat:@" 支付方式:%@",str];
                //        NSString * str3 = [NSString stringWithFormat:@"   支付ID:%@",[payDic objectForKey:@"SalePayID"]];
                [payStr appendString:str1] ;
                [payStr appendString:str2] ;
                //        [payStr appendString:str3] ;
                [mlArrReturn addObject:payStr];
            }
            [mlDicReturn setValue:mlArrReturn forKey:@"PayEntitys"];
            
            
            mlArrReturn=[[NSMutableArray alloc]init];
            
            for (NSDictionary * promotionDic in temppromotionEntitysArr) {
                NSMutableString * promotionStr = [[NSMutableString alloc]init] ;
                
                NSString * str1 ;NSString * str2 ;NSString * str3 ;NSString * str4 ;
                //        NSString * sstr = [NSString stringWithFormat:@"sdf  , 促销活动名称:%@  ",@"ss"];
                if ([promotionDic objectForKey:@"PromotionRuleID"] !=  [NSNull null] ) {
                    str1 = [NSString stringWithFormat:@"  促销活动ID:%@ ,",[promotionDic objectForKey:@"PromotionRuleID"]];
                } else {
                    
                    str1 = @"  促销活动ID: 无   ,";
                }
                
                
                
                if ([promotionDic objectForKey:@"PresentName"] !=  [NSNull null] ) {
                    str2 = [NSString stringWithFormat:@"     促销信息:%@  ,",[promotionDic objectForKey:@"PresentName"]];
                } else {
                    
                    str2 = @"   促销信息: 无    ,";
                }
                if ([promotionDic objectForKey:@"PresentDescription"] !=  [NSNull null] ) {
                    str3 = [NSString stringWithFormat:@"     促销方式:%@  ,",[promotionDic objectForKey:@"PresentDescription"]];
                } else {
                    
                    str3 = @"    促销方式: 无    ,";
                }
                if ([promotionDic objectForKey:@"DiscountMoney"] !=  [NSNull null] ) {
                    
                    NSString * rateStr =[promotionDic objectForKey:@"DiscountMoney"] ;
                    
                    str4 = [NSString stringWithFormat:@"    折扣金额:%.2f,",[rateStr floatValue]];
                } else {
                    
                    str4 = @"   折扣金额: 无 .";
                }
                
                [promotionStr appendString: str1 ] ;
                [promotionStr appendString: str2 ] ;
                [promotionStr appendString: str3 ] ;
                [promotionStr appendString: str4 ] ;
                [mlArrReturn addObject:promotionStr] ;
                
            }
            [mlDicReturn setValue:mlArrReturn forKey:@"PromotionEntitys"];
            temppromotionEntitysArr=nil;
            tempPayEntitys=nil;
            mlArrReturn=nil;
        }
        processSuccess(mlDicReturn);
    }
    else{
        [DicRequest ShowAlert:@"错误" Message:[responseData objectForKey:@"ErrMessage"]];
    }
    
    mlDicReturn=nil;
    

}
-(void)dealloc{
    baseserver.baseclient=nil;
    baseserver=nil;
}
@end
